package jmine.tec.web.wicket.component;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.WebComponent;
import org.apache.wicket.markup.transformer.XsltTransformer;

/**
 * @author wagner
 */
public class XSTLTransformingComponent extends WebComponent {
    private final String xslFilePath;

    private final String xmlContent;

    /**
     * C'tor.
     * 
     * @param id id do componente.
     * @param xstlPath file path
     * @param xml the xml to transform
     */
    public XSTLTransformingComponent(String id, String xstlPath, String xml) {
        super(id);
        this.xslFilePath = xstlPath;
        this.xmlContent = xml;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
        try {
            this.replaceComponentTagBody(markupStream, openTag, new XsltTransformer(this.xslFilePath).transform(this,
                    this.xmlContent));
        } catch (Exception e) {
            this.error(e.getMessage());
        }
    }
}
